package cn.iocoder.yudao.module.order.service.memberstream;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.order.controller.admin.memberstream.vo.*;
import cn.iocoder.yudao.module.order.dal.dataobject.memberstream.MemberStreamDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.order.convert.memberstream.MemberStreamConvert;
import cn.iocoder.yudao.module.order.dal.mysql.memberstream.MemberStreamMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.order.enums.ErrorCodeConstants.*;

/**
 * 会员流水 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MemberStreamServiceImpl implements MemberStreamService {

    @Resource
    private MemberStreamMapper memberStreamMapper;

    @Override
    public Integer createMemberStream(MemberStreamCreateReqVO createReqVO) {
        // 插入
        MemberStreamDO memberStream = MemberStreamConvert.INSTANCE.convert(createReqVO);
        memberStreamMapper.insert(memberStream);
        // 返回
        return memberStream.getId();
    }

    @Override
    public void updateMemberStream(MemberStreamUpdateReqVO updateReqVO) {
        // 校验存在
        validateMemberStreamExists(updateReqVO.getId());
        // 更新
        MemberStreamDO updateObj = MemberStreamConvert.INSTANCE.convert(updateReqVO);
        memberStreamMapper.updateById(updateObj);
    }

    @Override
    public void deleteMemberStream(Integer id) {
        // 校验存在
        validateMemberStreamExists(id);
        // 删除
        memberStreamMapper.deleteById(id);
    }

    private void validateMemberStreamExists(Integer id) {
        if (memberStreamMapper.selectById(id) == null) {
            throw exception(MEMBER_STREAM_NOT_EXISTS);
        }
    }

    @Override
    public MemberStreamDO getMemberStream(Integer id) {
        return memberStreamMapper.selectById(id);
    }

    @Override
    public List<MemberStreamDO> getMemberStreamList(Collection<Integer> ids) {
        return memberStreamMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MemberStreamDO> getMemberStreamPage(MemberStreamPageReqVO pageReqVO) {
        return memberStreamMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MemberStreamDO> getMemberStreamList(MemberStreamExportReqVO exportReqVO) {
        return memberStreamMapper.selectList(exportReqVO);
    }

}
